<?php

function mPrint($val)
{
  // echo "\r\n<pre>\r\n";
  // print_r($val);
  // echo "\r\n</pre>\r\n";
  echo json_encode($val, JSON_UNESCAPED_UNICODE);
}
function mPrintX($val)
{
  // echo "\r\n<pre>\r\n";
  // var_dump($val);
  // echo "\r\n</pre>\r\n";
  die(json_encode($val, JSON_UNESCAPED_UNICODE));
}
function randomString($len = 32, $number = false)
{
  $len = (int) $len;
  $chars = 'ABCDEFGHIJKLMBOPQRSTUFWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  if ($number) $chars = '0123456789';
  $maxPos = strlen($chars) - 1;
  $pwd = '';
  for ($i = 0; $i < $len; $i++) {
    $pwd .= substr($chars, rand(0, $maxPos), 1);
  }
  return $pwd;
}

function pid2tree($data, $pid = 0)
{
  $tree = [];
  foreach ($data as $value) {
    if ($value['pid'] == $pid) {
      $temp = pid2tree($data, $value['id']);
      if (count($temp) > 0) {
        $value['children'] = $temp;
      }
      $tree[] = $value;
    }
  }
  return $tree;
}
function tree2arr($params, $key = false)
{
  $tmp = $params;
  unset($tmp['children']);
  $ret = [$key ? $tmp[$key] : $tmp];
  if (is_array($params['children'])) {
    foreach ($params['children'] as $v) {
      $ret = array_merge($ret, tree2arr($v, $key));
    }
  }
  return $ret;
}

function id2item($items, $id, $retI = false)
{
  $num = count($items);
  for ($i = 0; $i < $num; $i++) {
    if ($items[$i]['id'] == $id) {
      return $retI ? $i : $items[$i];
    }
  }
  return $retI ? -1 : false;
}
function v2item($items, $v, $key = 'id', $retI = false)
{
  if (is_array($items)) {
    $num = count($items);
    for ($i = 0; $i < $num; $i++) {
      if ($items[$i][$key] == $v) return $retI ? $i : $items[$i];
      if (
        !$retI &&
        isset($items[$i]['children']) &&
        is_array($items[$i]['children'])
      ) {
        $tmp = v2item($items[$i]['children'], $v, $key);
        if ($tmp) return $tmp;
      }
    }
  }
  return $retI ? -1 : false;
}
function arrCMP($a, $b)
{
  if (isArray($a) && isArray($b)) {
    foreach ($a as $k => $v) {
      if ($v != $b[$k]) return false;
    }
    return true;
  }
  return false;
}
function getItem($arr, $val, $retI = false)
{
  if (sureArray($arr) && isArray($val)) {
    foreach ($arr as $k => $v) {
      if (arrCMP($val, $v)) {
        if (is_string($retI)) return $v[$retI];
        elseif ($retI === 0) return $k;
        else return $v;
      }
    }
  }
  return ($retI === 0 ? -1 : false);
}
function delArr($arr, $k = false)
{
  if (is_string($k) && $k) $k = [$k];

  if ($k) $k = array_merge($k, ['changeTime', 'createTime', 'useTime']);
  else {
    $k = ['changeTime', 'createTime', 'useTime'];
  }
  $j = count($k);
  for ($i = 0; $i < $j; $i++) {
    unset($arr[$k[$i]]);
  }
  return $arr;
}
function cpArr($arr, $k)
{
  if (is_string($k) && $k) $k = [$k];
  $j = count($k);
  $ret = [];
  for ($i = 0; $i < $j; $i++) {
    $ret[$k[$i]] = $arr[$k[$i]];
  }
  return $ret;
}

function n2str($n, $len = 2)
{
  $ret = (string) $n;
  $l = strlen($ret);
  while ($len > $l) {
    $l++;
    $ret = '0' . $ret;
  }
  return substr($ret, $l - $len);
}

function isArray($val)
{
  return isset($val) && is_array($val);
};
function sureArray($val, $order = true)
{
  if (isArray($val)) {
    $keys = array_keys($val);
    $i = 0;
    foreach ($keys as $v) {
      if (!is_numeric($v) || $v > $i) return false;
      if ($order) $i++;
      else $i = $v;
    }
    return true;
  }
  return false;
};

function pushPid($arr, $id, $pushKey = false)
{
  $ret = [];
  if (sureArray($arr)) {
    $ret = tree2arr(v2item(pid2tree($arr), $id), $pushKey);
  }
  return $ret;
};

function GetArrKeys($arr, $key = 'id')
{
  $ret = [];
  if (sureArray($arr)) {
    foreach ($arr as $value) {
      $ret[] = $value[$key];
    }
  }
  return $ret;
}

function abs_minus($val)
{
  return ~abs((int) $val) + 1;
}
function arrKeyIn($arr, $val, $key = 'id')
{
  if (sureArray($arr)) {
    foreach ($arr as $v) {
      if ($v[$key] == $val) return $v;
    }
  }
  return false;
};

function PushKey(&$arr, $key = 'id')
{
  if (sureArray($arr)) {
    foreach ($arr as $k => $v) {
      $arr[$k] = $v[$key];
    }
  }
  return $arr;
}

function CheckKey($arr, $keys)
{
  $k = array_keys($arr);
  if (count($k) < count($keys)) return false;
  foreach ($keys as $v) {
    if (!in_array($v, $k)) return false;
  }
  return true;
}
